package com.asset.dispoal.channel.service.common;

import com.asset.dispoal.channel.domain.db.entity.SysChannelDebt;
import com.asset.dispoal.channel.domain.db.mapper.SysChannelMapper;
import com.asset.dispoal.channel.dto.SysChannelDebtRequest;
import com.asset.dispoal.channel.dto.SysChannelDebtResponse;
import com.asset.dispoal.channel.service.converter.SysChannelConverter;
import com.asset.dispoal.commom.page.PagingQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @author qr
 * @date 2022/3/25$
 */
@Component
public class SysChannelDebtQueryPageCommand implements BaseCommand<SysChannelDebtRequest, Result<PageVO<SysChannelDebtResponse>>> {


    @Autowired
    SysChannelMapper sysChannelMapper;

    @Override
    public Result<PageVO<SysChannelDebtResponse>> execute(SysChannelDebtRequest request) {
        SysChannelDebt sysChannel = SysChannelConverter.INSTANCE.resDebtToDo(request);
        int count = sysChannelMapper.countDebt(sysChannel);
        Page<SysChannelDebt> page = PagingQuery.getPage(count, sysChannel);
        PageVO<SysChannelDebtResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            List<SysChannelDebt> mapperList = sysChannelMapper.pageDebt(page);
            List<SysChannelDebtResponse> sysChannelResponses = SysChannelConverter.INSTANCE.dosDebtToRes(mapperList);
            pageVO.setResult(sysChannelResponses);
        }
        return Result.ok(pageVO);
    }
}
